package com.tendory.gpssim.service.jt808;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;

public class MsgOutHandler extends ChannelOutboundHandlerAdapter {
    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        byte[] networkMessage = (byte[]) msg;
//        final String sessionId = Session.buildId(ctx.channel(), networkMessage.getRemoteAddress());
//        Session session = sessionManager.findBySessionId(sessionId);
//        if (session != null) {
//            ByteBuf res = session.encode(ctx, networkMessage.getMessage());
//            if (res == null) {
//                throw new Exception("No msg send");
//            }
//            ctx.write(new NetworkMessage(res, networkMessage.getRemoteAddress()), promise);
//            return;
//        }
        if (networkMessage != null) {
            ByteBuf res = Unpooled.buffer();
            res.writeBytes(networkMessage);
            ctx.write(res);
            return;
        }
        super.write(ctx, msg, promise);
    }
}
